package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/protobuf/types/known/emptypb"
	"mxshop_srvs/goods_srv/proto"
)

var categoryClient proto.GoodsClient
var conn *grpc.ClientConn

func main() {
	Init()
	//TestGetAllCategorysList()
	//TestCreateCategory()
	//TestUpdateCategory()
	GetSubCategory()
	//TestDeleteBanner()
	//conn.Close()
}

func TestUpdateCategory() {
	banner, err := categoryClient.UpdateCategory(context.Background(), &proto.CategoryInfoRequest{
		Id:   238015,
		Name: "BB食品",
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(banner)
}

/*
*
创建分类
*/
func TestCreateCategory() {
	category, err := categoryClient.CreateCategory(context.Background(), &proto.CategoryInfoRequest{
		Name:           "BB食品",
		ParentCategory: 135202,
		Level:          2,
		IsTab:          false,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(category)
}

/*
*
获取一级分类下的所有分类
*/
func TestGetAllCategorysList() {
	list, err := categoryClient.GetAllCategorysList(context.Background(), &emptypb.Empty{})
	if err != nil {
		panic(err)
	}
	fmt.Println(list.Total)
	data := list.JsonData
	fmt.Println(data)
}

func TestDeleteBanner() {
	banner, err := categoryClient.DeleteCategory(context.Background(), &proto.DeleteCategoryRequest{
		Id: 238015,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(banner)
}

func GetSubCategory() {
	subCategory, err := categoryClient.GetSubCategory(context.Background(), &proto.CategoryListRequest{
		Id: 135202,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(subCategory)
}

// 初始化
func Init() {
	var err error
	conn, err := grpc.Dial("192.168.31.78:50052", grpc.WithInsecure())
	if err != nil {
		panic(err)
	}
	categoryClient = proto.NewGoodsClient(conn)
}
